package com.stary.springmvc.web.rest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @description:
 * @author: Mr.Guan
 * @create: 2023-10-05 14:54
 * BookHandler用于处理rest请求
 **/
@Controller
@RequestMapping(value = "/user")
public class BookHandler {
    //查询get
    @RequestMapping(value = "/book/{id}",method = RequestMethod.GET)
    public String getBook(@PathVariable("id") String id){
        System.out.println("id = " + id);
        return "success";
    }

    //添加post
    @PostMapping(value = "/book")
    public String addBook(String bookName){
        System.out.println("bookName = " + bookName);
        return "success";
    }

    //删除delete
    /***
     * 注意：前端rest和rest1有笔记
     * ！！！没问题，前端js路径配置错误
     */
    // @DeleteMapping(value = "/book/{id}")
    // public String deleteBook(@PathVariable("id") String id){
    //     System.out.println("删除书籍：id = " + id);
    //     return "success";
    // }
    @RequestMapping(value = "/book/{id}", method = RequestMethod.DELETE)
    public String delBook(@PathVariable("id") String id) {
        System.out.println("删除书籍 id= " + id);
        //return "success"; //[如果这样返回会报错 JSPs only permit GET POST or HEAD]
        //但无需特别注意，以后不用json
        //原因：JSP只允许支持GET、POST和HEAD请求方法
        //1. redirect:/user/success重定向
        //2. 会被解析成 /springmvc/user/success
        return "redirect:/user/success";
    }
    //修改[PUT]
    @PutMapping(value = "/book/{id}")
    public String updateBook(@PathVariable("id") String id) {
        System.out.println("修改书籍 id=" + id);
        return "redirect:/user/success";
    }

    //如果请求是/user/success,就转发到 success.jsp 页面
    @RequestMapping(value = "/success")
    public String successGenecal() {
        return "success"; //由该方法 转发到 success.jsp 页面
    }
}
